<!DOCTYPE html>
<html>
<head>
<title>Particle Motion Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="500" height="500"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Style parameters
    const axisColor = '#000';
    const gridColor = '#d3d3d3';
    const particleColor = '#000';
    const vectorColor = '#000';
    const textColor = '#000';
    const fontLabels = 'bold 22px Arial';
    const fontTicks = '20px Arial';

    // Coordinate system parameters
    const padding = 50;
    const xMax = 5;
    const yMax = 5;

    const canvasWidth = canvas.width;
    const canvasHeight = canvas.height;
    const graphWidth = canvasWidth - 2 * padding;
    const graphHeight = canvasHeight - 2 * padding;

    // Transformation functions
    function toCanvasX(x) {
        return padding + (x / xMax) * graphWidth;
    }

    function toCanvasY(y) {
        return canvasHeight - padding - (y / yMax) * graphHeight;
    }

    // Clear canvas
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvasWidth, canvasHeight);


    // --- Drawing ---

    // 1. Draw Grid
    ctx.strokeStyle = gridColor;
    ctx.lineWidth = 1;
    for (let i = 1; i <= xMax; i++) {
        ctx.beginPath();
        ctx.moveTo(toCanvasX(i), toCanvasY(0));
        ctx.lineTo(toCanvasX(i), toCanvasY(yMax));
        ctx.stroke();
    }
    for (let i = 1; i <= yMax; i++) {
        ctx.beginPath();
        ctx.moveTo(toCanvasX(0), toCanvasY(i));
        ctx.lineTo(toCanvasX(xMax), toCanvasY(i));
        ctx.stroke();
    }

    // 2. Draw Axes
    ctx.strokeStyle = axisColor;
    ctx.lineWidth = 2;
    ctx.beginPath();
    ctx.moveTo(toCanvasX(0), toCanvasY(0));
    ctx.lineTo(toCanvasX(xMax), toCanvasY(0)); // x-axis
    ctx.moveTo(toCanvasX(0), toCanvasY(0));
    ctx.lineTo(toCanvasX(0), toCanvasY(yMax)); // y-axis
    ctx.stroke();

    // 3. Draw Axis Ticks and Labels
    ctx.fillStyle = textColor;
    ctx.font = fontTicks;
    
    // X-axis ticks and numbers
    ctx.textAlign = 'center';
    ctx.textBaseline = 'top';
    ctx.fillText('0', toCanvasX(0), toCanvasY(0) + 5);
    for (let i = 1; i <= xMax; i++) {
        ctx.fillText(i, toCanvasX(i), toCanvasY(0) + 5);
        ctx.beginPath();
        ctx.moveTo(toCanvasX(i), toCanvasY(0) - 4);
        ctx.lineTo(toCanvasX(i), toCanvasY(0) + 4);
        ctx.stroke();
    }

    // Y-axis ticks and numbers
    ctx.textAlign = 'right';
    ctx.textBaseline = 'middle';
    for (let i = 1; i <= yMax; i++) {
        ctx.fillText(i, toCanvasX(0) - 5, toCanvasY(i));
        ctx.beginPath();
        ctx.moveTo(toCanvasX(0) - 4, toCanvasY(i));
        ctx.lineTo(toCanvasX(0) + 4, toCanvasY(i));
        ctx.stroke();
    }
    
    // 4. Draw Axis Titles
    ctx.font = fontLabels;
    ctx.textAlign = 'left';
    ctx.textBaseline = 'top';
    ctx.fillText('y (m)', toCanvasX(0.2), toCanvasY(5.2));
    
    ctx.textAlign = 'center';
    ctx.textBaseline = 'top';
    ctx.fillText('x (m)', toCanvasX(4.5), toCanvasY(-0.5));

    // 5. Draw Dashed Trajectory Line
    ctx.beginPath();
    ctx.setLineDash([5, 5]);
    ctx.strokeStyle = axisColor;
    ctx.lineWidth = 1.5;
    ctx.moveTo(toCanvasX(0), toCanvasY(4));
    ctx.lineTo(toCanvasX(3), toCanvasY(0));
    ctx.stroke();
    ctx.setLineDash([]); // Reset to solid line

    // 6. Draw Particle
    const particleX = 1;
    const particleY = -4/3 * particleX + 4; // y = 8/3 ≈ 2.67
    ctx.beginPath();
    ctx.arc(toCanvasX(particleX), toCanvasY(particleY), 7, 0, 2 * Math.PI);
    ctx.fillStyle = particleColor;
    ctx.fill();

    // 7. Draw Velocity Vector
    function drawArrow(ctx, fromX, fromY, toX, toY) {
        const headlen = 15;
        const angle = Math.atan2(toY - fromY, toX - fromX);
        ctx.lineWidth = 3;
        ctx.strokeStyle = vectorColor;
        ctx.fillStyle = vectorColor;

        // Line
        ctx.beginPath();
        ctx.moveTo(fromX, fromY);
        ctx.lineTo(toX, toY);
        ctx.stroke();

        // Arrowhead
        ctx.beginPath();
        ctx.moveTo(toX, toY);
        ctx.lineTo(toX - headlen * Math.cos(angle - Math.PI / 7), toY - headlen * Math.sin(angle - Math.PI / 7));
        ctx.lineTo(toX - headlen * Math.cos(angle + Math.PI / 7), toY - headlen * Math.sin(angle + Math.PI / 7));
        ctx.closePath();
        ctx.fill();
    }
    
    const arrowEndX = 2.0;
    const arrowEndY = -4/3 * arrowEndX + 4; // y = 4/3 ≈ 1.33
    drawArrow(
        ctx,
        toCanvasX(particleX),
        toCanvasY(particleY),
        toCanvasX(arrowEndX),
        toCanvasY(arrowEndY)
    );

    // 8. Draw Text Labels for particle
    ctx.fillStyle = textColor;
    ctx.font = fontLabels;
    ctx.textAlign = 'left';
    ctx.textBaseline = 'bottom';
    ctx.fillText('2 kg', toCanvasX(1.2), toCanvasY(3.0));
    ctx.fillText('v = 5 m/s', toCanvasX(1.2), toCanvasY(2.3));

</script>
</body>
</html>